<?php
// +----------------------------------------------------------------------
// | Description: 客户
// +----------------------------------------------------------------------
// | Author: zl
// +----------------------------------------------------------------------

namespace app\crm\controller;

use app\admin\controller\ApiCommon;
use app\admin\model\PersonModel;
use app\admin\model\ShopModel;
use app\crm\model\BalanceLog;
use app\crm\model\CustomerPrestockGoods;
use app\crm\model\Setting;
use app\crm\model\SettingCategory;
use app\crm\model\Service;
use app\crm\model\ReturnVist;
use app\crm\model\ReturnVistT;
use app\crm\model\Customer as CustomerModel;
use app\crm\model\TransferShop;
use app\mini\model\SysModelMini;
use app\crm\model\CustomerConfig as CustomerConfigModel;
use app\crm\model\CustomerGrade as CustomerGradeModel;
use app\mini\model\SysModelMiniCustomer;
use think\response\Json;
use think\facade\Db;
use Kingdee\Kingdee;
class CustomerPrestock extends ApiCommon
{
    protected $customerModel;
    protected $customerConfigModel;
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize()
    {
        $action = [
            'permission' => ['house_list','setcustomerage','setzycustomer','dsmonth','setmonth'],
            'allow' => ['users', 'info']
        ];
        event('CheckAuth',$action);
        $request = $this->request;
        $a = strtolower($request->action());

        $this->customerModel = new CustomerModel();
        $this->customerConfigModel = new CustomerConfigModel();
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }
    public function miniIndex(): Json
    {
        $res = Db::name('mini_index')->where('id',1)->find();

        return resultArray(['error' => '', 'data'=>['content'=>$res['wx_index']]]);
    }

    /**
     * 顾客预存货列表
     */
    public function prestockList(): json
    {
        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $this->param['user_id'] = $this->userInfo['id'];
        $customer  = new CustomerModel();

        $data = $customer->prestockList($this->param);

        return resultArray(['data' => $data]);
    }

    /**
     * 出货单-出货-查看单商品列表
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function prestockGoodslist(CustomerPrestockGoods $customerPrestockGoods): Json
    {
        try{
            $data = $customerPrestockGoods->prestockGoodslist($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 出货单-出货-查看单商品列表
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function prostockShipmentVerify(CustomerPrestockGoods $customerPrestockGoods): Json
    {
        try{
            $data = $customerPrestockGoods->prostockShipmentVerify($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 出货
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function shipment(CustomerPrestockGoods $CustomerPrestockGoods): Json
    {
        try{
            $this->param['shop_id'] = $this->userInfo['shop_id']; //当前操作人员所属门店
            $this->param['operator_id'] = $this->userInfo['id'];
            $this->param['operator_name'] = $this->userInfo['username'];
            $res = $CustomerPrestockGoods->shipment($this->param);
            return resultArray(['data'=>[],'error'=>'出货成功']);
        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 编辑
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function update(CustomerModel $customerModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $customerModel->saveData($this->param);
        if (!$data) {
            return resultArray(['error' => $customerModel->getError()]);
        }
        return resultArray(['error' => $customerModel->getError(), 'data'=>[]]);
    }

    /**
     * 预存货明细
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function prestockGoodslistDetail(CustomerPrestockGoods $CustomerPrestockGoods){
        try{
            $this->param['shop_id'] = $this->userInfo['shop_id']; //当前操作人员所属门店
            $data = $CustomerPrestockGoods->prestockGoodslistDetail($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 预存货明细
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function shopPrestockList(CustomerPrestockGoods $CustomerPrestockGoods){
        try{
            $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();
            $data = $CustomerPrestockGoods->shopPrestockList($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 预存货明细-导出
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function preListExit(CustomerPrestockGoods $CustomerPrestockGoods){
        try{

            $shopIds = $this->getCommonShopIdsCollectByUserType();
            $this->param['shopIds'] = $shopIds;
            $data = $CustomerPrestockGoods->preListExit($this->param);
            if (!$data) {
                return resultArray(['error' => $CustomerPrestockGoods->getError()]);
            }
            $callName = [
                'market_name' =>'市场名称',
                'shop_name' =>'门店名称',
                'customer_name' =>'顾客姓名',
                'deal_time' =>'存货日期',
                'goods_name' =>'产品名称',
                'goods_code' =>'产品编码',
                'pay_price' =>'销售单价',
                'takeout_stock_num' =>'存货总数',
                'sales_amount' =>'销售总额',
                'shipment_num' =>'出货数',
                'shipment_amount' =>'出货总额',
                'residual_shipment_num' =>'剩余存货数',
                'residual_sales_amount' =>'剩余预存金额',
                'business_type'=>'业务类型'
            ];
            if($data['list']){
                $downUrl = exportExcelData('顾客预存明细',$callName,$data['list'],'xlsx',1);
                return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
                exit;
            }else{
                return resultArray(['error' => '请选择导出门店！！！']);
            }

        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 门店预存货明细
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function shopPrestockGoodsList(CustomerPrestockGoods $CustomerPrestockGoods){
        try{
            $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();
            $data = $CustomerPrestockGoods->shopPrestockGoodsList($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 门店预存货商品列表-明细
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function shopPrestockGoodsDateil(CustomerPrestockGoods $CustomerPrestockGoods){
        try{
            $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();
            $data = $CustomerPrestockGoods->shopPrestockGoodsDateil($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 服务列表
     * @param  Service $CustomerPrestockGoods
     * @return Json
     */
    public function preLogExport(CustomerPrestockGoods $CustomerPrestockGoods): Json
    {

        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $data = $CustomerPrestockGoods->preLogExport($this->param);
        if (!$data) {
            return resultArray(['error' => $CustomerPrestockGoods->getError()]);
        }
        $callName = [
            'shop_name' =>'门店名称',
            'customer_name' =>'顾客姓名',
            'goods_name' =>'商品名称',
            'takeoutStockNum'=>'预存数量',
            'prestock_num' =>'剩余数量',
            'deal_times'=>'预存时间',
        ];
        if($data['list']){
            $downUrl = exportExcelData('单商品预存明细',$callName,$data['list'],'xlsx',1);
            return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
            exit;
        }else{
            return resultArray(['error' => '请选择导出门店！！！']);
        }
    }

    /**
     * 服务列表
     * @param  Service $CustomerPrestockGoods
     * @return Json
     */
    public function preGoodsExport(CustomerPrestockGoods $CustomerPrestockGoods): Json
    {

        $shopIds = $this->getCommonShopIdsCollectByUserType();
        $this->param['shopIds'] = $shopIds;
        $data = $CustomerPrestockGoods->preGoodsExport($this->param);
        if (!$data) {
            return resultArray(['error' => $CustomerPrestockGoods->getError()]);
        }
        $callName = [
            'goods_name' =>'商品名称',
            'kingdee_code' =>'商品编码',
            'prestock_num' =>'预存总数',
            'num'=>'剩余数量',
        ];
        if($data['list']){
            $downUrl = exportExcelData('预存商品明细',$callName,$data['list'],'xlsx',1);
            return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
            exit;
        }else{
            return resultArray(['error' => '请选择导出门店！！！']);
        }
    }

    /**
     * 门店预存货商品列表-退出存货数明细
     * @param  CustomerModel $customerModel
     * @return Json
     */
    public function refundOutPrestockDateil(CustomerPrestockGoods $CustomerPrestockGoods){
        try{
            $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();
            $data = $CustomerPrestockGoods->refundOutPrestockDateil($this->param);
            return resultArray(['data' => $data]);
        }catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }






}
